package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.enums.BizDataStatusEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.*;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.upmp.vo.TBNotifyInfoVO;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.system.service.UserService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author System
 * @since 2022-04-16
 */
@Slf4j
@Api(tags = "系统管理:公告管理")
@RestController
@RequestMapping("/gen/tbnotifyinfo")
public class TBNotifyController extends BaseController {
    @Autowired
    private NtcAdvMchntAreaOrgservice ntcAdvMchntAreaOrgService;
    @Autowired
    private TBNotifyInfoService tbNotifyInfoService;
    @Autowired
    private IUpmpBMchntBaseInfoService iUpmpBMchntBaseInfoService;
    @Autowired
    private IUpmpBTradeAreaInfoService iUpmpBTradeAreaInfoService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Log("分页公告")
    @GetMapping
//    @RequiresPermissions("NotifyInfo:view")
    public Map<String, Object> list(QueryRequest request, TBNotifyInfo entity) {
        return getDataTable(this.tbNotifyInfoService.findByPage(request, entity));
    }


    @Log("下载导入模板")
    @PostMapping("/getExcelTemplate")
    public RedpResponse getExportTemplate(HttpServletResponse response) {
        try {

            String fileName = URLEncoder.encode("mchntNoBatchImportTemplate.xlsx", StandardCharsets.UTF_8.toString());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

            List<List<String>> headList = new ArrayList<>();
            List<String> head = new ArrayList<>();
            head.add("商户编号");
            headList.add(head);
            EasyExcel.write(response.getOutputStream()).head(headList).autoCloseStream(true).sheet("模板")    .doWrite(null);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }
        return RedpResponse.getInstance().success().message("导出Excel成功");
    }

    @Log("导入商户表")
    @PostMapping("/uploadntc")
    @ResponseBody
    public RedpResponse getDataFromExcelNtc(@RequestParam(value = "file", required = false) MultipartFile excelFile) throws Exception {
        return tbNotifyInfoService.uploadntc(excelFile);
    }

    @Log("导入excel")
    @PostMapping(value = "import")
    public List<NtcAdvMchntAreaOrg> importExcelData(@RequestParam("file") MultipartFile file) throws IOException {
        // 构造要批量插入的NtcAdvMchntAreaOrg对象
        List<NtcAdvMchntAreaOrg> ntcAdvMchntAreaOrg = new ArrayList<>();
        // 通过文件输入流读取到对应的 workbook 工作簿
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        // 只解析第一张 sheet 工作表
        XSSFSheet sheet = workbook.getSheetAt(0);
        // 遍历第一个工作表的所有行
        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
            if (i == 0) continue; // 跳过标题行

            XSSFRow row = sheet.getRow(i); // 获取工作表中的某一行，通过下标获取
            if (row == null) continue; // 跳过空行
            // 遍历一个行中的所有列
            for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
                XSSFCell cell = row.getCell(j); // 获取一行中的某个单元格，通过下标获取
                if (cell == null) continue; // 跳过空单元格
                // 获取单元格中的字符串内容
                String cellValue = cell.getStringCellValue();
                // 获取单元格中的数字内容
                double cellValue2 = cell.getNumericCellValue();
                // 判断单元格是第几个，从零开始
                switch (j) {
                    case 0: // 第一列就是姓名
                        ntcAdvMchntAreaOrg.get(i).setSubjectNo(cellValue); // 给实体类的setter属性赋值
                        break;
                }
            }
        }
        return ntcAdvMchntAreaOrg;
    }

    @Log("导出商户信息")
    @PostMapping("excel")
    public RedpResponse export(@Valid TBNotifyInfoVO notifyExcleId, HttpServletResponse response) {
        try {
//            QueryWrapper bmchntWrapper = new QueryWrapper<>();
            List<NtcAdvMchntAreaOrg> ntcAdvMchntAreaOrg = new ArrayList<>();
            for (int i = 0; i < notifyExcleId.getCircleList().size(); i++) {
                String notifyString = notifyExcleId.getCircleList().get(i).toString();
                String substring = "";
                int b = 0;
//                List<String> list=new ArrayList<>();
                if (notifyString.indexOf("\n") > 0) {
                    for (int j = 0; j < notifyString.length(); ) {
                        NtcAdvMchntAreaOrg ntcAdvMchntAreaOrg1 = new NtcAdvMchntAreaOrg();
                        int a = notifyString.indexOf("\n");
                        if (notifyString.indexOf("\n") > 0) {
                            substring = notifyString.substring(b, a);
                            ntcAdvMchntAreaOrg1.setSubjectNo(substring);
                            notifyString = notifyString.replace(substring + "\n", "");
                            ntcAdvMchntAreaOrg.add(ntcAdvMchntAreaOrg1);

                            j = j + a;
                        }
                    }
                }
                if (notifyString.indexOf("\n") < 0) {
                    NtcAdvMchntAreaOrg ntcAdvMchntAreaOrg1 = new NtcAdvMchntAreaOrg();
                    substring = notifyString;
                    ntcAdvMchntAreaOrg1.setSubjectNo(substring);
                    ntcAdvMchntAreaOrg.add(ntcAdvMchntAreaOrg1);
                }
            }
            ExcelKit.$Export(NtcAdvMchntAreaOrg.class, response).downXlsx(ntcAdvMchntAreaOrg, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }
        return RedpResponse.getInstance().success().message("导出Excel成功");
    }

    @Log("商户查询")
    @PostMapping("circle")
    public TBNotifyInfoVO circle(@Valid TBNotifyInfoVO notifyExcleId) throws RedpException {
        TBNotifyInfoVO notifyInfoVO = new TBNotifyInfoVO();
        try {
            List<NtcAdvMchntAreaOrg> ntcAdvMchntAreaOrg = new ArrayList<>();
            List<String> stringList = new ArrayList<>();
            for (int i = 0; i < notifyExcleId.getCircleList().size(); i++) {
                String notifyString = notifyExcleId.getCircleList().get(i).toString();
                String substring = "";
                int b = 0;
                if (notifyString.indexOf("\n") > 0) {
                    for (int j = 0; j <= notifyString.length(); ) {
                        int a = notifyString.indexOf("\n");
                        substring = notifyString.substring(b, a);
                        QueryWrapper bmchntWrapper = new QueryWrapper<>();
                        bmchntWrapper.eq("MCHNT_NO", substring);
                        UpmpBMchntBaseInfo one = iUpmpBMchntBaseInfoService.getOne(bmchntWrapper);
                        String mchntName = one.getMchntName() + "(" + notifyString + ")";
                        stringList.add(mchntName);
                        notifyInfoVO.setOprId("0");
                        notifyString = notifyString.replace(substring + "\n", "");
                        j = j + a;
                        notifyInfoVO.setRemark1(notifyInfoVO.getRemark1()+1);
                    }
                }
                if (notifyString.indexOf("\n") < 0) {
                    QueryWrapper bmchntWrapper = new QueryWrapper<>();
                    bmchntWrapper.eq("MCHNT_NO", notifyString);
                    UpmpBMchntBaseInfo one = iUpmpBMchntBaseInfoService.getOne(bmchntWrapper);
                    String mchntName = one.getMchntName() + "(" + notifyString + ")";
                    stringList.add(mchntName);
                    notifyInfoVO.setRemark1(notifyInfoVO.getRemark1()+1);
                    notifyInfoVO.setOprId("0");
                }
            }
            notifyInfoVO.setCircleList(stringList);
        } catch (Exception e) {
            String message = "没有找到该商户";
            log.error(message, e);
            throw new RedpException(message);
        }
        return notifyInfoVO;
    }

    @Log("商圈查询")
    @PostMapping("Merchant")
    public TBNotifyInfoVO Merchant(@Valid TBNotifyInfoVO notifyExcleId) throws RedpException {
        TBNotifyInfoVO notifyInfoVO = new TBNotifyInfoVO();
        try {
            List<NtcAdvMchntAreaOrg> ntcAdvMchntAreaOrg = new ArrayList<>();
            List<String> stringList = new ArrayList<>();
            for (int i = 0; i < notifyExcleId.getMerchantList().size(); i++) {
                String notifyString = notifyExcleId.getMerchantList().get(i).toString();
                String substring = "";
                int b = 0;
                    for (int j = 0; j <= notifyString.length(); ) {
                        if (notifyString.indexOf("\n") > 0) {
                        int a = notifyString.indexOf("\n");
                        substring = notifyString.substring(b, a);
                        QueryWrapper bmchntWrapper = new QueryWrapper<>();
                        bmchntWrapper.eq("TRADE_AREA_CODE", substring);
                        UpmpBTradeAreaInfo one = iUpmpBTradeAreaInfoService.getOne(bmchntWrapper);
                        String mchntName = one.getTradeAreaCode() + "(" + substring + ")";
                        stringList.add(mchntName);
                        notifyInfoVO.setOprId("1");
                        notifyString = notifyString.replace(substring + "\n", "");
                            notifyInfoVO.setRemark2(notifyInfoVO.getRemark2()+1);
                        j = j + a;
                        } else if (notifyString.indexOf("\n") < 0) {
                            QueryWrapper bmchntWrapper = new QueryWrapper<>();
                            bmchntWrapper.eq("TRADE_AREA_CODE", notifyString);
                            UpmpBTradeAreaInfo one = iUpmpBTradeAreaInfoService.getOne(bmchntWrapper);
                            String mchntName = one.getTradeAreaCode() + "(" + substring + ")";
                            stringList.add(mchntName);
                            notifyInfoVO.setOprId("1");
                            notifyInfoVO.setRemark2(notifyInfoVO.getRemark2()+1);
                            break;
                        }
                    }
            }
        } catch (Exception e) {
            String message = "没有找到该商圈";
            log.error(message, e);
            throw new RedpException(message);
        }
        return notifyInfoVO;
    }

    @Log("商户商圈查询")
    @GetMapping("/notifyInfo/{notifyid}")
    public TBNotifyInfoVO NotifyInfo(@PathVariable String notifyid) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ntcadv_no", notifyid);
        TBNotifyInfo byId = tbNotifyInfoService.findById(notifyid);
        TBNotifyInfoVO tbNotifyInfoVO = new TBNotifyInfoVO();
        tbNotifyInfoVO.setTitle(byId.getTitle());
        tbNotifyInfoVO.setCreateTime(byId.getCreateTime());
        tbNotifyInfoVO.setExpireTime(byId.getExpireTime());
        tbNotifyInfoVO.setMessage(byId.getMessage());
        tbNotifyInfoVO.setId(byId.getId());
        tbNotifyInfoVO.setBeginTime(byId.getBeginTime());
        List<NtcAdvMchntAreaOrg> list = ntcAdvMchntAreaOrgService.list(queryWrapper);
        List<String> stringList = new ArrayList<>();
        List<String> mchntList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getSubjectTyp().equals("0")) {
                    String notifyString = list.get(i).getSubjectNo();
                    String substring = "";
                    int b = 0;
                    for (int j = 0; j <= notifyString.length(); ) {
                        if (notifyString.indexOf("\n") > 0) {
                            int a = notifyString.indexOf("\n");
                            substring = notifyString.substring(b, a);
                            QueryWrapper bmchntWrapper = new QueryWrapper<>();
                            bmchntWrapper.eq("MCHNT_NO", notifyString);
                            UpmpBMchntBaseInfo one = iUpmpBMchntBaseInfoService.getOne(bmchntWrapper);
                            String mchntName ="";
                            if(one != null){
                                mchntName = one.getMchntName() + "(" + substring + ")";
                                stringList.add(mchntName);
                            }
//                            stringList.add(mchntName);
                            tbNotifyInfoVO.setOprId("0");
                            tbNotifyInfoVO.setRemark1(tbNotifyInfoVO.getRemark1()+1);
                            notifyString = notifyString.replace(substring + "\n", "");
                            j = j + a;
                        } else if (notifyString.indexOf("\n") < 0) {
                            QueryWrapper bmchntWrapper = new QueryWrapper<>();
                            bmchntWrapper.eq("MCHNT_NO", notifyString);
                            UpmpBMchntBaseInfo one = iUpmpBMchntBaseInfoService.getOne(bmchntWrapper);
                            String mchntName = "";
                            if(one != null){
                                mchntName = one.getMchntName() + "(" + notifyString + ")";
                                stringList.add(mchntName);
                            }

                            tbNotifyInfoVO.setOprId("0");
                            tbNotifyInfoVO.setRemark1(tbNotifyInfoVO.getRemark1()+1);
                            break;
                        }
                    }
                    tbNotifyInfoVO.setCircleList(stringList);
                } else {
                    String notifyString =  list.get(i).getSubjectNo();
                    String substring = "";
                    int b = 0;
                    for (int j = 0; j <= notifyString.length(); ) {
                        if (notifyString.indexOf("\n") > 0) {
                            int a = notifyString.indexOf("\n");
                            substring = notifyString.substring(b, a);
                            QueryWrapper bmchntWrapper = new QueryWrapper<>();
                            bmchntWrapper.eq("TRADE_AREA_CODE", substring);
                            UpmpBTradeAreaInfo one = iUpmpBTradeAreaInfoService.getOne(bmchntWrapper);
                            String mchntName ="";
                            if(one != null){
                                mchntName = one.getTradeAreaCode() + "(" + substring + ")";
                                mchntList.add(mchntName);
                            }

                            tbNotifyInfoVO.setOprId("1");
                            notifyString = notifyString.replace(substring + "\n", "");
                            j = j + a;
                            tbNotifyInfoVO.setRemark2(tbNotifyInfoVO.getRemark2()+1);
                        } else if (notifyString.indexOf("\n") < 0) {
                            QueryWrapper bmchntWrapper = new QueryWrapper<>();
                            bmchntWrapper.eq("TRADE_AREA_CODE", notifyString);
                            UpmpBTradeAreaInfo one = iUpmpBTradeAreaInfoService.getOne(bmchntWrapper);
                            String mchntName = "";
                            if(one != null){
                                mchntName =one.getTradeAreaName() + "(" + notifyString + ")";
                                mchntList.add(mchntName);
                            }

                            tbNotifyInfoVO.setOprId("1");
                            tbNotifyInfoVO.setRemark2(tbNotifyInfoVO.getRemark2()+1);
                            break;
                        }
                    }
                    tbNotifyInfoVO.setMerchantList(mchntList);
                }
            }

        return tbNotifyInfoVO;
    }

    @GetMapping("getAll")
    public List<TBNotifyInfo> getAll(TBNotifyInfo role) {
        return tbNotifyInfoService.getAllNotify(role);
    }

    @Log("按条件查询")
    @GetMapping("check/{instId}/{roleName}")
    public List<TBNotifyInfo> checkNotifyName(@NotBlank(message = "{required}") @PathVariable String instId, @NotBlank(message = "{required}") @PathVariable String roleName) {
        return this.tbNotifyInfoService.findByName(instId, roleName);
    }

    @Log("新增通知")
    @PostMapping
    @RequiresPermissions("notify:add")
    public void addNotify(@Valid TBNotifyInfoVO notifyInfoVO) throws RedpException {
        List circleList = notifyInfoVO.getCircleList();
        List merchantList = notifyInfoVO.getMerchantList();
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
//        YamlEncryptGenerator
        try {
            TBNotifyInfo notify = new TBNotifyInfo();
            UUID notifyuuid = UUID.randomUUID();
            String notifystr = notifyuuid.toString();
            String notifytemp = notifystr.substring(0, 8) + notifystr.substring(9, 13) + notifystr.substring(14, 18) + notifystr.substring(19, 23) + notifystr.substring(24);
            notify.setId(notifytemp);
            //还获取当前用户信息
            UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
            notify.setCrtOrgId(user.getCrtOrgId());
            notify.setOprId(user.getLoginId());
            notify.setCreateTime(lastUpddt);
            notify.setInstId(notifyInfoVO.getInstId());
            notify.setTitle(notifyInfoVO.getTitle());
            notify.setBeginTime(notifyInfoVO.getBeginTime());
            notify.setExpireTime(notifyInfoVO.getExpireTime());
            notify.setMessage(notifyInfoVO.getMessage());
            notify.setNotifyStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
            notify.setAuditStatus(BusinessContainer.agentStatus_01);
            OperationResult operationResult = this.tbNotifyInfoService.createNotify(notify);
            String id = (String) operationResult.getData();

            //循环保存商圈与公告的关系
            if (circleList != null && circleList.size() > 0) {
                for (int i = 0; i < circleList.size(); i++) {
                    NtcAdvMchntAreaOrg entityNtcDev = new NtcAdvMchntAreaOrg();
                    entityNtcDev.setInstId(notifyInfoVO.getInstId());
                    UUID uuid = UUID.randomUUID();
                    String str = uuid.toString();
                    // 去掉"-"符号
                    String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
                    entityNtcDev.setId(temp);
                    entityNtcDev.setNtcadvNo(id);
                    entityNtcDev.setNtcadvTyp("0");
                    entityNtcDev.setSubjectNo((String) circleList.get(i));
                    entityNtcDev.setSubjectTyp("0");
                    ntcAdvMchntAreaOrgService.save(entityNtcDev);
                }
            }
            //循环保存商户与公告的关系
            if (merchantList != null && merchantList.size() > 0) {
                for (int i = 0; i < merchantList.size(); i++) {
                    NtcAdvMchntAreaOrg entityNtcDev = new NtcAdvMchntAreaOrg();
                    entityNtcDev.setInstId(notifyInfoVO.getInstId());
                    UUID uuid = UUID.randomUUID();
                    String str = uuid.toString();
                    // 去掉"-"符号
                    String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
                    entityNtcDev.setId(temp);
                    entityNtcDev.setNtcadvNo(id);
                    entityNtcDev.setNtcadvTyp("0");
                    entityNtcDev.setSubjectNo((String) merchantList.get(i));
                    entityNtcDev.setSubjectTyp("1");
                    ntcAdvMchntAreaOrgService.save(entityNtcDev);
                }
            }
        } catch (Exception e) {
            String message = "新增通知失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("公告审核")
    @PostMapping("auditNotify")
//    @RequiresPermissions("notify:add")
    public void auditNotify(@Valid String notifyid, String notify) throws RedpException {
        tbNotifyInfoService.auditNotify(notifyid, notify);
    }


    @Log("删除通知")
    @DeleteMapping("/{NotifyIds}")
    @RequiresPermissions("notify:delete")
    public RedpResponse deleteNotify(@NotBlank(message = "{required}") @PathVariable String NotifyIds) throws RedpException {
        try {
            String[] ids = NotifyIds.split(StringPool.COMMA);
            List<String> list = Arrays.asList(ids);
            for (int i = 0; i < list.size(); i++) {
                QueryWrapper<NtcAdvMchntAreaOrg> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("NTCADV_NO", list.get(i));
                //删除关联表中的数据
                ntcAdvMchntAreaOrgService.remove(queryWrapper);
            }

            this.tbNotifyInfoService.deleteNotify(ids);


        } catch (Exception e) {
            String message = "删除公告失败";
            log.error(message, e);
//
            throw new RedpException(message);
        }
        return RedpResponse.getInstance().success().message("删除公告成功");
    }

    @Log("修改通知")
    @PutMapping
    @RequiresPermissions("notify:update")
    public void updateNotify(TBNotifyInfoVO notifyInfoVO) throws RedpException {
        try {
            TBNotifyInfo notify = new TBNotifyInfo();
            notify.setId(notifyInfoVO.getId());
            String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
            notify.setLastUpdateTime(lastUpddt);
            notify.setInstId(notifyInfoVO.getInstId());
            notify.setTitle(notifyInfoVO.getTitle());
            notify.setBeginTime(notifyInfoVO.getBeginTime());
            notify.setExpireTime(notifyInfoVO.getExpireTime());
            notify.setMessage(notifyInfoVO.getMessage());
            notify.setNotifyStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
            notify.setAuditStatus(BusinessContainer.agentStatus_01);
            this.tbNotifyInfoService.updateNotify(notify);
            TBNotifyInfo byId = this.tbNotifyInfoService.findById(notify.getId());

                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("NTCADV_NO", byId.getId());
                List<NtcAdvMchntAreaOrg> list = this.ntcAdvMchntAreaOrgService.list(queryWrapper);
                    List circleList = new ArrayList();
                List mchntList = new ArrayList();
                int circlea = 0;
                int mchntb = 0;
                for (int i = 0; i < list.size(); i++) {
                    NtcAdvMchntAreaOrg ntcAdvMchntcircle = (NtcAdvMchntAreaOrg) list.get(i);
                    if (ntcAdvMchntcircle.getSubjectTyp().equals("0")) {
                        circleList.add(ntcAdvMchntcircle);
                        circlea++;
                    } else if (ntcAdvMchntcircle.getSubjectTyp().equals("1")) {
                        mchntb++;
                        mchntList.add(ntcAdvMchntcircle);
                    }
                }
            if (notifyInfoVO.getCircleList()!=null) {
                if (circlea != 0) {
                    for (int i = 0; i < circleList.size(); i++) {
                        NtcAdvMchntAreaOrg ntcAdvMchntAreaOrg = (NtcAdvMchntAreaOrg) circleList.get(i);
                        if (ntcAdvMchntAreaOrg.getSubjectTyp().equals("0")) {
                            NtcAdvMchntAreaOrg entityNtcDev = new NtcAdvMchntAreaOrg();
                            entityNtcDev.setId(ntcAdvMchntAreaOrg.getId());
                            entityNtcDev.setNtcadvNo(notifyInfoVO.getId());
                            entityNtcDev.setInstId(ntcAdvMchntAreaOrg.getInstId());
                            entityNtcDev.setNtcadvTyp("0");
                            String notitydev = (String) notifyInfoVO.getCircleList().get(i);
                            if (notitydev.indexOf("(") > 0) {
//                        String notidev=notitydev.substring(notitydev.indexOf("(")+1,notitydev.indexOf(")"));
//                        entityNtcDev.setSubjectNo(notidev);
                                entityNtcDev.setSubjectNo(null);
                            } else {
                                entityNtcDev.setSubjectNo(notitydev);
                            }
                            entityNtcDev.setSubjectTyp("0");
                            QueryWrapper areaOrgWrapper = new QueryWrapper();
                            areaOrgWrapper.eq("ID", entityNtcDev.getId());
                            ntcAdvMchntAreaOrgService.update(entityNtcDev, areaOrgWrapper);
                        }
                    }
                } else {
                    //循环保存商圈与公告的关系
                    circleList.add(notifyInfoVO.getCircleList());
                    for (int i = 0; i < circleList.size(); i++) {
                        NtcAdvMchntAreaOrg entityNtcDev = new NtcAdvMchntAreaOrg();
                        entityNtcDev.setInstId(notifyInfoVO.getInstId());
                        UUID uuid = UUID.randomUUID();
                        String str = uuid.toString();
                        // 去掉"-"符号
                        String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
                        entityNtcDev.setId(temp);
                        entityNtcDev.setNtcadvNo(notify.getId());
                        entityNtcDev.setNtcadvTyp("0");
                        entityNtcDev.setSubjectNo((String) notifyInfoVO.getCircleList().get(i));
                        entityNtcDev.setSubjectTyp("0");
                        ntcAdvMchntAreaOrgService.save(entityNtcDev);
                    }

                }
            }
            if (notifyInfoVO.getMerchantList()!=null) {
                if (mchntb != 0) {
                    for (int i = 0; i < mchntList.size(); i++) {
                        NtcAdvMchntAreaOrg ntcAdvMchntAreaOrg = (NtcAdvMchntAreaOrg) mchntList.get(i);
                        if (ntcAdvMchntAreaOrg.getSubjectTyp().equals("1")) {
                            NtcAdvMchntAreaOrg entityNtcDev = new NtcAdvMchntAreaOrg();
                            entityNtcDev.setId(ntcAdvMchntAreaOrg.getId());
                            entityNtcDev.setNtcadvNo(notifyInfoVO.getId());
                            entityNtcDev.setInstId(ntcAdvMchntAreaOrg.getInstId());
                            entityNtcDev.setNtcadvTyp("0");
                            String notitydev = (String) notifyInfoVO.getMerchantList().get(i);
                            if (notitydev.indexOf("(") > 0) {
//                            String notidev=notitydev.substring(notitydev.indexOf("(")+1,notitydev.indexOf(")"));
//                            entityNtcDev.setSubjectNo(notidev);
                                entityNtcDev.setSubjectNo(null);
                            } else {
                                entityNtcDev.setSubjectNo(notitydev);
                            }
                            entityNtcDev.setSubjectTyp("1");
                            QueryWrapper merchantWrapper = new QueryWrapper();
                            merchantWrapper.eq("ID", entityNtcDev.getId());
                            ntcAdvMchntAreaOrgService.update(entityNtcDev, merchantWrapper);
                        }
                    }
                } else {
                    //循环保存商户与公告的关系
                    mchntList.add(notifyInfoVO.getMerchantList());
                    for (int i = 0; i < mchntList.size(); i++) {
                        NtcAdvMchntAreaOrg entityNtcDev = new NtcAdvMchntAreaOrg();
                        entityNtcDev.setInstId(notifyInfoVO.getInstId());
                        UUID uuid = UUID.randomUUID();
                        String str = uuid.toString();
                        // 去掉"-"符号
                        String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
                        entityNtcDev.setId(temp);
                        entityNtcDev.setNtcadvNo(notify.getId());
                        entityNtcDev.setNtcadvTyp("0");
                        entityNtcDev.setSubjectNo((String) notifyInfoVO.getMerchantList().get(i));
                        entityNtcDev.setSubjectTyp("1");
                        ntcAdvMchntAreaOrgService.save(entityNtcDev);
                    }
                }
            }
        } catch (Exception e) {
            String message = "修改通知失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }
}
